package com.cskaoyan.bean;

import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;

public class GenerateProxyInstance {

    public static void main(String[] args) {
        //生成代理类的字节码文件 → 和委托类是相同的包目录
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY,
                "D:\\WorkSpace\\j34_workspace\\codes\\day01-design-pattern\\demo6-cglib-dynamic-proxy");

        WangDaiYi wangDaiYi = new WangDaiYi();
        //代理类和委托类之间的关系：代理类继承了委托类
        //cglib动态代理可以使用委托类来接收代理对象
        WangDaiYi proxy = (WangDaiYi) Enhancer.create(WangDaiYi.class, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object invoke = method.invoke(wangDaiYi, args);
                System.out.println("一根烤肠嗷嗷");
                return invoke;
            }
        });
        proxy.sayHello("学姐");
    }
}
